<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="120px"
      v-loading="formLoading"
    >
      <el-form-item label="用户昵称" prop="nickname">
        <el-input v-model="formData.nickname" disabled />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="formData.mobile" disabled />
      </el-form-item>
      <el-form-item label="认证状态" prop="certificationStatus">
        <el-radio-group v-model="formData.certificationStatus" disabled>
          <el-radio
            v-for="dict in getStrDictOptions(DICT_TYPE.MEMBER_CERTIFICATION_STATUS)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="会员真实名称" prop="realName">
        <el-input v-model="formData.realName" placeholder="请输入会员真实名称" disabled />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCardNo">
        <el-input v-model="formData.idCardNo" placeholder="请输入身份证号" disabled />
      </el-form-item>
      <el-form-item label="身份证人像面" prop="idCardFaceImg">
        <img :src="formData.idCardFaceImg" style="width: 100px" @click="openImage([formData.idCardFaceImg])" />
      </el-form-item>
      <el-form-item label="身份证国徽面" prop="idCardNationalEmblemImg">
        <img :src="formData.idCardNationalEmblemImg" style="width: 100px" @click="openImage([formData.idCardFaceImg])" />
      </el-form-item>
      <el-form-item label="营业执照号" prop="licenseNo">
        <el-input v-model="formData.licenseNo" placeholder="请输入营业执照号" disabled />
      </el-form-item>
      <el-form-item label="营业执照照片" prop="licenseImg">
        <img :src="formData.licenseImg" style="width: 100px" @click="openImage([formData.idCardFaceImg])" />
      </el-form-item>
      <el-form-item label="银行卡号" prop="bankCardNo">
        <el-input v-model="formData.bankCardNo" placeholder="请输入银行卡号" disabled />
      </el-form-item>
      <el-form-item label="银行卡照片" prop="bankCardImg">
        <img :src="formData.bankCardImg" style="width: 100px" @click="openImage([formData.idCardFaceImg])" />
      </el-form-item>
      <el-form-item v-if="formType === 'update'" label="驳回原因" prop="rejectReason">
        <el-input type="textarea" v-model="formData.rejectReason" placeholder="请输入驳回原因" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button v-if="formType === 'update'" @click="auditForm('pass')" type="primary" :disabled="formLoading">通 过</el-button>
      <el-button v-if="formType === 'update'" @click="auditForm('reject')" :disabled="formLoading">驳 回</el-button>
      <el-button v-if="formType === 'view'" @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import * as CertificationInfoApi from '@/api/member/certificationinfo'
import { DICT_TYPE, getStrDictOptions } from '@/utils/dict'
import { createImageViewer } from '@/components/ImageViewer'

const openImage = (imageArray) => {
  createImageViewer({
    zIndex: 9999999,
    urlList: imageArray
  })
}

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  nickname: undefined,
  mobile: undefined,
  id: undefined,
  userId: undefined,
  certificationStatus: undefined,
  realName: undefined,
  idCardNo: undefined,
  idCardFaceImg: undefined,
  idCardNationalEmblemImg: undefined,
  licenseNo: undefined,
  licenseImg: undefined,
  bankCardNo: undefined,
  bankCardImg: undefined,
  rejectReason: undefined,
})
const formRules = reactive({
  userId: [{ required: true, message: '用户编号不能为空', trigger: 'blur' }],
  certificationStatus: [{ required: true, message: '认证状态, 0-未认证，1-认证中，2-已认证不能为空', trigger: 'blur' }],
  realName: [{ required: true, message: '会员真实名称不能为空', trigger: 'blur' }],
  idCardNo: [{ required: true, message: '身份证号不能为空', trigger: 'blur' }],
  idCardFaceImg: [{ required: true, message: '身份证人像面不能为空', trigger: 'blur' }],
  idCardNationalEmblemImg: [{ required: true, message: '身份证国徽面不能为空', trigger: 'blur' }],
  licenseNo: [{ required: true, message: '营业执照号不能为空', trigger: 'blur' }],
  licenseImg: [{ required: true, message: '营业执照照片不能为空', trigger: 'blur' }],
  bankCardNo: [{ required: true, message: '银行卡号不能为空', trigger: 'blur' }],
  bankCardImg: [{ required: true, message: '银行卡照片不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = type === 'view' ? '查看' : '审核'
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id !== undefined && id !== null) {
    formLoading.value = true
    try {
      formData.value = await CertificationInfoApi.getCertificationInfo(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as CertificationInfoApi.CertificationInfoVO
    if (formType.value === 'create') {
      await CertificationInfoApi.createCertificationInfo(data)
      message.success(t('common.createSuccess'))
    } else {
      await CertificationInfoApi.updateCertificationInfo(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 审核表单 */
const auditForm = async (type:string) => {
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as CertificationInfoApi.CertificationInfoAuditVO
    if (type === 'pass') {
      data.certificationStatus = '2'
    } else {
      data.certificationStatus = '0'
    }
    await CertificationInfoApi.auditCertificationInfo(data)
    message.success(t('common.operateSuccess'))
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    nickname: undefined,
    mobile: undefined,
    id: undefined,
    userId: undefined,
    certificationStatus: undefined,
    realName: undefined,
    idCardNo: undefined,
    idCardFaceImg: undefined,
    idCardNationalEmblemImg: undefined,
    licenseNo: undefined,
    licenseImg: undefined,
    bankCardNo: undefined,
    bankCardImg: undefined,
    rejectReason: undefined,
  }
  formRef.value?.resetFields()
}
</script>
